//  this library.
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/model/my/local_file_model.dart';
import 'package:hktk_app/pages/preview/image_preview_page.dart';
import 'package:hktk_app/pages/preview/pdf_preview_page.dart';
import 'package:hktk_app/pages/preview/txt_preview_page.dart';
import 'package:hktk_app/util/font_util.dart';

import 'package:hktk_app/util/toast_util.dart';
import 'package:hktk_app/util/view_util.dart';

typedef DownLoadBtnFun = void Function(BuildContext context);

/// 下载详情展示
class DownloadDetailWidget extends StatelessWidget {
  /// 相关数据
  final LocalFileModel model;

  const DownloadDetailWidget({
    super.key,
    required this.model,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 240,
      padding: EdgeInsets.all(18),
      decoration: BoxDecoration(
        color: colorWhite1,
        borderRadius: BorderRadius.circular(20),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _iconItem(),
          _rightItems(context),
        ],
      ),
    );
  }

  /// 左边图标显示
  _iconItem() {
    return Container(
      // width: 148,
      // height: 204,
      padding: EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: colorWhite1,
        borderRadius: BorderRadius.circular(16),
        border: Border.all(color: colorGrey16, width: 1),
      ),
      child: _iconConItem(),
    );
  }

  _iconConItem() {
    String type = model.type;
    if (type == "png" || type == "jpg" || type == "jpeg") {
      return _imageItem();
    } else {
      return _officeItem();
    }
  }

  _imageItem() {
    String url = model.path;
    return Image.file(
      File(url),
      width: 124,
      height: 180,
      fit: BoxFit.fill,
    );
  }

  _officeItem() {
    String svgUrl = _getSvgUrl();
    return Stack(
      children: [
        Container(
          width: 80,
          height: 120,
          decoration: BoxDecoration(
            border: Border.all(color: colorBlack9, width: 1),
          ),
          child: Image.asset(
            "images/my/download_detail_image.png",
            width: 80,
            height: 120,
          ),
        ),
        if (svgUrl != "")
          Positioned(
            bottom: 4,
            right: 4,
            child: SvgPicture.asset(
              svgUrl,
              width: 40,
              // height: 30,
            ),
          ),
      ],
    );
  }

  /// 获取对应类型的图标路径
  _getSvgUrl() {
    String type = model.type;
    if (type == "docx") {
      return "images/my/download_detail_doc.svg";
    } else if (type == "ppt") {
      return "images/my/download_detail_ppt.svg";
    } else if (type == "txt") {
      return "images/my/download_detail_txt.svg";
    } else if (type == "pdf") {
      return "images/my/download_detail_pdf.svg";
    } else if (type == "xls") {
      return "images/my/download_detail_xls.svg";
    }
    return "";
  }

  /// 右边组件显示
  _rightItems(BuildContext context) {
    return SizedBox(
      width: 1.sw-80-110,
      height: 176,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _titleItem(),
          _bottomItems(context),
        ],
      ),
    );
  }

  /// 标题显示
  _titleItem() {
    return SizedBox(
      width: 1.sw-80-110,
      height: 84,
      child: Text(
        model.name,
        style: FontUtil.fontBlack1(22, fontWeight: FontWeight.w500),
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
      ),
    );
  }

  /// 底部组件组
  _bottomItems(BuildContext context) {
    return SizedBox(
      height: 76,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          _btnItem(
            context: context,
            btnText: "查看",
            btnFun: _onTapFunXz,
          ),
        ],
      ),
    );
  }

  /// 按钮显示
  _btnItem({
    required BuildContext context,
    required String btnText,
    required DownLoadBtnFun btnFun,
  }) {
    return GestureDetector(
      onTap: () {
        debugPrint("downloadDetailWidget 组件 -> 你点击了 ===> $btnText");
        btnFun(context);
      },
      child: Container(
        width: 112,
        height: 56,
        decoration: BoxDecoration(
          color: colorBlue1,
          borderRadius: BorderRadius.circular(34),
        ),
        child: Center(
          child: Padding(
            padding: EdgeInsets.only(bottom: 3),
            child: Text(
              btnText,
              style: FontUtil.fontWhile1(26),
            ),
          ),
        ),
      ),
    );
  }

  /// 下载按钮点击方法
  void _onTapFunXz(BuildContext context) {
    debugPrint("你点击了 ===> 查看");
    // 跳转资源页面查看
    bool showToast = false;
    switch (model.type) {
      case "pdf":
        Navigator.pushNamed(context, "/preview-pdf",arguments: {
          "url": model.path,
          "name": model.name,
          "type": model.type,
          "netWork": false,
        });

        break;
      case "doc":
      case "docx":
      case "xls":
      case "xlsx":
      // 由于doc没有下载选项，没有相关的下载内容？
      // _showDocFun(context: context);
        break;
      case "jpg":
      case "jpeg":
      case "png":
      Navigator.pushNamed(context, "/preview-img",arguments: {
        "url": model.path,
        "name": model.name,
        "type": model.type,
        "netWork": false,
      });
        break;
      case "txt":
        Navigator.pushNamed(context, "/preview-txt",arguments: {
          "url": model.path,
          "name": model.name,
          "type": model.type,
          "netWork": false,
        });

        break;
      default:
        showToast = true;
    }
    if (showToast) {
      ToastUtil.show(context: context, msg: "不支持的文件类型");
    }
  }

// /// 显示打开本地应用软件
// void _showDocFun({
//   required BuildContext context,
// }) async {
//   // 打开加载框
//   ToastUtil.showLoadingDialog(context);
//   _futureWord().then((Map<String, dynamic> map) {
//     // 关闭加载框
//     NavigatorUtil.pop(context);
//     if (!map["suc"]) {
//       ToastUtil.show(context: context, msg: map["msg"]);
//     }
//   });
// }
//
// /// 打开对应本地文件
// Future<Map<String, dynamic>> _futureWord() async {
//   Map<String, dynamic> map = {};
//   try {
//     // 打开对应本地文件
//     String type = model.type;
//     String fileType;
//     if (Platform.isIOS) {
//       String sType = type;
//       if (type == "docx") sType = "doc";
//       if (type == "xlsx") sType = "xls";
//       fileType = iosMap[sType]!;
//     } else {
//       fileType = androidMap[type]!;
//     }
//     OpenResult result = await OpenFile.open(model.path, type: fileType);
//     debugPrint("result ===> $result");
//     debugPrint("type ===> ${result.type}");
//     debugPrint("message ===> ${result.message}");
//     if (result.type == ResultType.noAppToOpen) {
//       map["suc"] = false;
//       map["msg"] = "找不到打开此文件的app";
//     } else if (result.type == ResultType.permissionDenied) {
//       map["suc"] = false;
//       map["msg"] = "权限被拒绝";
//     } else if (result.type == ResultType.error ||
//         result.type == ResultType.fileNotFound) {
//       map["suc"] = false;
//       map["msg"] = "未知错误，请重试";
//     } else {
//       map["suc"] = true;
//     }
//   } catch (e) {
//     debugPrint("word打开本地应用发生异常 ===> $e");
//     map = {"suc": false, "msg": "未知错误，请重试"};
//     if (e is DaoException) {
//       debugPrint(e.msg);
//       map["msg"] = e.msg;
//     }
//   }
//   return map;
// }
}
